package com.kkd.kkdapp.activity;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.android_async_http.AsyncHttpResponseHandler;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.manager.HttpCallBack;

import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.DialogUtils;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;


import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

/**
 * 设置交易密码
 * 
 * @author mty
 * 
 */
public class SetDealActivity extends BaseActivity implements OnClickListener {

	private EditText withdraw_ps; // 取现密码
	private EditText setdeal_ed; // 确认密码
	private Button set_deal_bt; // 下一步
	// private String version; // 当前App版本号
	private RelativeLayout set_deal_img; // 返回
	private boolean SUBMIT = false;
	private boolean LOGOINFLAG = true;
	private Handler handler = new Handler();
	private int setFlag;
	private Dialog loaDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_set_deal);
		inintView();
		setListener();
	}
	
	@Override
	protected void onDestroy() {
		if (loaDialog != null) {
			loaDialog.cancel();
			loaDialog = null;
		}
		super.onDestroy();
	}

	/**
	 * 初始化组件
	 */
	public void inintView() {
		withdraw_ps = (EditText) this.findViewById(R.id.withdraw_ps);
		setdeal_ed = (EditText) this.findViewById(R.id.setdeal_ed);
		set_deal_bt = (Button) this.findViewById(R.id.set_deal_bt);
		set_deal_img = (RelativeLayout) this.findViewById(R.id.set_deal_img);

	}

	public void getMessage() {
		Intent intent = this.getIntent();
		setFlag = intent.getIntExtra("setFlag", -1);
	}

	/**
	 * 注册监听
	 */
	public void setListener() {
		set_deal_bt.setOnClickListener(this);
		set_deal_img.setOnClickListener(this);
		withdraw_ps.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if ("".equals(withdraw_ps.getText().toString())
						|| "".equals(setdeal_ed.getText().toString())) {
					set_deal_bt.setEnabled(false);
					SUBMIT = false;
				} else {

					set_deal_bt.setEnabled(true);
					SUBMIT = true;
				}

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
		setdeal_ed.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if ("".equals(withdraw_ps.getText().toString())
						|| "".equals(setdeal_ed.getText().toString())) {
					set_deal_bt.setEnabled(false);
					SUBMIT = false;
				} else {
					set_deal_bt.setEnabled(true);
					SUBMIT = true;
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.set_deal_bt: // 下一步
			if (SUBMIT) {
				if (LOGOINFLAG) {
					LOGOINFLAG = false;
					handler.postDelayed(new Runnable() {

						@Override
						public void run() {
							LOGOINFLAG = true;
						}
					}, 2000);
					if (userstringFilter(withdraw_ps.getText().toString())) {
						setPayPassword();
					} else {
						showToast(R.string.withdrawpassword_on);
					}
				}
			}
			break;
		case R.id.set_deal_img: // 返回
			finish();
			break;
		}

	}

	public void setPayPassword() {
		if (loaDialog != null) {
			loaDialog.cancel();
			loaDialog = null;
		}
		loaDialog = DialogUtils.toLoadDialog(this, "努力加载中....");

		KkdHttpMagager.getInstance().getSetDealManager(
				withdraw_ps.getText().toString(),
				setdeal_ed.getText().toString(),  String.class,
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {

						try {
							if (setFlag == 1) {
								Intent intent = new Intent(
										SetDealActivity.this,
										WithdrawActivity.class);
								startActivity(intent);
								finish();
							} else {
								finish();
							}
						} catch (Exception e) {

							e.printStackTrace();
						}
						if (loaDialog != null) {
							loaDialog.dismiss();
						}
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						showToast(desc);
						if (loaDialog != null) {
							loaDialog.dismiss();
						}
					}
				});
	}
}
